package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.model.type.EGroupPushType;
import com.hangzhou.xyj.model.type.EMsgHandleResultType;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.multiimageselector.utils.TimeUtils;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class SystemMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<GroupPushMsgModel> mMessageList;
    private OnMessageItemClickListener mClickListener;

    public SystemMessageAdapter(Context mContext, List<GroupPushMsgModel> messageList) {
        this.mContext = mContext;
        if (messageList == null) {
            messageList = new ArrayList<>();
        }
        this.mMessageList = messageList;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setOnMessageItemClickListener(OnMessageItemClickListener listener) {
        this.mClickListener = listener;
    }

    public void update(List<GroupPushMsgModel> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mMessageList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mMessageList.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        GroupPushMsgModel msgModel = mMessageList.get(position);

        ((MyViewHolder) viewHolder).setData(msgModel, position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new MyViewHolder(mInflater.inflate(R.layout.layout_system_message_item, viewGroup, false));
    }

    public interface OnMessageItemClickListener {
        void onAgreeClick(GroupPushMsgModel model, int position);

        void onRefuseClick(GroupPushMsgModel model, int position);

        void onItemClick(GroupPushMsgModel model, int position);
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        CircleImageView mIvImage;
        MyTextView mTvName;
        MyTextView mTvContent;
        View mVUnderline;
        private TextView mTvHandleTip;
        private View mItemView;
        private Button mBtnAgree;
        private Button mBtnRefuse;
        private GroupPushMsgModel mMsgModel;
        private int mPosition;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.mItemView = itemView;
            mIvImage = (CircleImageView) itemView.findViewById(R.id.civ_header);
            mTvName = (MyTextView) itemView.findViewById(R.id.mtv_name);
            mTvContent = (MyTextView) itemView.findViewById(R.id.mtv_content);
            mTvHandleTip = (MyTextView) itemView.findViewById(R.id.tv_msg_handle_tip);
            mBtnAgree = (Button) itemView.findViewById(R.id.btn_agree);
            mBtnRefuse = (Button) itemView.findViewById(R.id.btn_refuse);
            mVUnderline = itemView.findViewById(R.id.v_underline);

            initListeners();
        }

        private void initListeners() {
            //同意按钮
            mBtnAgree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mClickListener != null) {
                        mClickListener.onAgreeClick(mMsgModel, mPosition);
                    }
                }
            });
            //拒绝按钮
            mBtnRefuse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mClickListener != null) {
                        mClickListener.onRefuseClick(mMsgModel, mPosition);
                    }
                }
            });
            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mClickListener != null) {
                        mClickListener.onItemClick(mMsgModel, mPosition);
                    }
                }
            });
        }

        public void setData(GroupPushMsgModel model, int position) {
            this.mMsgModel = model;
            this.mPosition = position;
            EGroupPushType type = EGroupPushType.value(model.getMsgType());

            //头像
            String headUrl = "";
            switch (type) {
                case ASK:
                    headUrl = model.getPartUserHead();
                    break;
                case AGREE:
                case REFUSE:
                    headUrl = model.getGroupPic();
                    break;
            }
            mIvImage.displayImage(headUrl);

            //title
            String title = "";
            switch (type) {
                case ASK:
                    title = model.getPartUserName();
                    break;
                case AGREE:
                case REFUSE:
                    title = model.getGroupName();
                    break;
            }
            mTvName.setText(title);
            //消息内容
            String timeStr = TimeUtils.timeFormat(model.getMsgTime(), "HH:mm");
            String formatContent = "";
            switch (type) {
                case ASK:
                    formatContent = "%s 申请加入[%s]";
                    break;
                case AGREE:
                    formatContent = "%s 同意了您的加入";
                    break;
                case REFUSE:
                    formatContent = "%s 拒绝了您的加入";
                    break;
            }
            mTvContent.setText(String.format(formatContent, timeStr, model.getGroupName()));

            //消息显示
            switch (type) {
                case ASK: //申请消息:处理该消息是否处理
                    handleResultMsg(model.getHandleResult());
                    break;
                case AGREE: //下面消息只显示文本，没有处理信息
                case REFUSE:
                    mBtnAgree.setVisibility(View.GONE);
                    mBtnRefuse.setVisibility(View.GONE);
                    mTvHandleTip.setVisibility(View.GONE);
                    break;
            }


            //分割线
            if (position != 0 && position == mMessageList.size() - 1) {
                mVUnderline.setVisibility(View.GONE);
            } else {
                mVUnderline.setVisibility(View.VISIBLE);
            }
        }

        private void handleResultMsg(int result) {
            EMsgHandleResultType resultType = EMsgHandleResultType.value(result);
            switch (resultType) {
                case AGREEED: //已同意
                    mBtnAgree.setVisibility(View.GONE);
                    mBtnRefuse.setVisibility(View.GONE);
                    mTvHandleTip.setVisibility(View.VISIBLE);
                    mTvHandleTip.setText("已同意");
                    break;
                case REFUSEED: //已拒绝
                    mBtnAgree.setVisibility(View.GONE);
                    mBtnRefuse.setVisibility(View.GONE);
                    mTvHandleTip.setVisibility(View.VISIBLE);
                    mTvHandleTip.setText("已拒绝");
                    break;
                default: //未处理
                    //同意按钮:拒绝按钮
                    EGroupPushType type = EGroupPushType.value(mMsgModel.getMsgType());
                    switch (type) {
                        case ASK:
                            mBtnAgree.setVisibility(View.VISIBLE);
                            mBtnRefuse.setVisibility(View.VISIBLE);
                            mTvHandleTip.setVisibility(View.GONE);
                            break;
                        default:
                            mBtnAgree.setVisibility(View.GONE);
                            mBtnRefuse.setVisibility(View.GONE);
                            mTvHandleTip.setVisibility(View.VISIBLE);
                            break;
                    }
                    break;
            }
        }
    }

}
